import tkinter as tk
from tkinter import ttk
import pandas as pd
import data as gpdata
import matplotlib.pyplot as plt
import mplfinance as mpf


#
def Show(parent, code, name):
    global g_code
    global g_name

    g_code = code
    strType = gpdata.get_stock_type(g_code)
    if strType not in g_code:
        g_code = g_code + '.' + strType

    g_name = name
    toplevel = tk.Toplevel(parent)
    toplevel.title(name)

    toplevel.geometry("210x100")
    
    bottom_btn_k_real = tk.Button(toplevel,text='实时k线',bg='#7CCD7C',command=k_real_click)
    bottom_btn_k_his = tk.Button(toplevel,text='历史k线',bg='#7CCD7C',command=k_his_click)
    bottom_btn_base = tk.Button(toplevel,text='基本信息',bg='#7CCD7C',command=base_click)
    bottom_btn_finance = tk.Button(toplevel,text='财务信息',bg='#7CCD7C',command=finance_click)
    bottom_btn_cyq_chips = tk.Button(toplevel,text='筹码分布',bg='#7CCD7C',command=cyq_chips_click)

    bottom_btn_base.place(x=5 + 35* 0,y=20)
    bottom_btn_k_real.place(x=5 + 35* 2,y=20)
    bottom_btn_k_his.place(x=5 + 35* 4,y=20)

    bottom_btn_finance.place(x=5 + 35*0,y=55)
    bottom_btn_cyq_chips.place(x=5 + 35*2,y=55)
#
def k_real_click():
    df = gpdata.get_hour_min(g_code[0:-3], gpdata.get_date("%Y%m%d", 0), gpdata.get_date("%Y%m%d", 0))
    show_line(df, g_name)
    pass    
#
def k_his_click():
    df = gpdata.get_daily_datas(g_code, gpdata.get_date("%Y%m%d", -365), gpdata.get_date("%Y%m%d", 0))
    show_candle(df, g_name)
    pass
#
def base_click():
    pass
#
def finance_click():
    pass
#
def cyq_chips_click():
    df = gpdata.tushare_pro.cyq_chips(ts_code=g_code.upper(), trade_date='')
    x = []
    y = []
    size = len(df['price'])
    new_size = size / 100
    sum = 0
    for i in size:
        sum += df['percent'][i]
        if i % new_size == 0:
            x.append(df['price'][i])
            y.append(sum)
            sum = 0
        
    plt.bar(x, y)
    plt.show()

#显示分时图
def show_line(df, title):
    df.rename(columns={'时间': 'trade_date'}, inplace=True)
    df.rename(columns={'开盘': 'open'}, inplace=True)
    df.rename(columns={'最高': 'high'}, inplace=True)
    df.rename(columns={'最低': 'low'}, inplace=True)
    df.rename(columns={'收盘': 'close'}, inplace=True)
    df.rename(columns={'成交量': 'volume'}, inplace=True)

    df['trade_date'] = pd.to_datetime(df['trade_date'], format='%Y-%m-%d %H:%M:%S')
    df.rename(columns={'vol': 'volume'}, inplace=True)
    df = df[['trade_date', 'open', 'high', 'low', 'close', 'volume']]
    df.set_index('trade_date', drop=True, inplace=True)
    df.sort_values(by=['trade_date'], axis=0, ascending=True, inplace=True)

    # up 上涨颜色，down下跌颜色
    # edge 边框颜色
    # wick 上影线下影线颜色
    mc = mpf.make_marketcolors(up='red', down='green', edge='inherit', wick='inherit',volume='inherit')
    # base_mpf_style 格式有好多种‘binance’, ‘blueskies’, ‘brasil’, ‘charles’, ‘checkers’, ‘classic’, ‘default’, ‘mike’, ‘nightclouds’, ‘sas’, ‘starsandstripes’, ‘yahoo’
    # rc这里为了避免中文乱码
    s = mpf.make_mpf_style(base_mpf_style='binance', marketcolors=mc, rc={'font.family': 'SimHei', 'axes.unicode_minus': 'False'})
    mpf.plot(df, style=s, volume=True,type='line', title=title,datetime_format='%H:%M')


#显示日线图
def show_candle(df, title):
    df = df.drop(columns=['change', 'pct_chg'])
    df['trade_date'] = pd.to_datetime(df['trade_date'], format='%Y%m%d')
    df.rename(columns={'vol': 'volume'}, inplace=True)
    df = df[['trade_date', 'open', 'high', 'low', 'close', 'volume']]
    df.set_index('trade_date', drop=True, inplace=True)
    df.sort_values(by=['trade_date'], axis=0, ascending=True, inplace=True)
    
    # up 上涨颜色，down下跌颜色
    # edge 边框颜色
    # wick 上影线下影线颜色
    mc = mpf.make_marketcolors(up='red', down='green', edge='inherit', wick='inherit',volume='inherit')
    # base_mpf_style 格式有好多种‘binance’, ‘blueskies’, ‘brasil’, ‘charles’, ‘checkers’, ‘classic’, ‘default’, ‘mike’, ‘nightclouds’, ‘sas’, ‘starsandstripes’, ‘yahoo’
    # rc这里为了避免中文乱码
    s = mpf.make_mpf_style(base_mpf_style='binance', marketcolors=mc, rc={'font.family': 'SimHei', 'axes.unicode_minus': 'False'})
    mpf.plot(df, style=s, volume=True,type='candle', title=title,datetime_format='%Y-%m-%d',mav=(5,10,30))
